package com.kongwg.java;

import org.junit.Test;

import java.util.Random;

/**
 * @author kongwg
 * @create 2020-12-24 14:32
 */
public class NewInstanceTest {

    @Test
    public void test1() throws Exception {
        Class<Person> clazz1 = Person.class;
        Person instance = clazz1.newInstance();
        System.out.println(instance);

    }

    @Test
    public void test2(){
        for (int i = 0; i < 100; i++) {
            int random = new Random().nextInt(3);
            String classPath=null;
            switch (random){
                case 0:
                    classPath = "java.util.Date";
                    break;
                case 1:
                    classPath = "java.lang.Object";
                    break;
                case 2:
                    classPath = "com.kongwg.java.Person";
                    break;
            }
            try {
                Object obj = getNewInstance(classPath);
                System.out.println(obj);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public Object getNewInstance(String classPath) throws Exception {
        Class<?> clazz = Class.forName(classPath);
        Object obj = clazz.newInstance();
        return obj;



    }
}
